/**
 * Copyright &copy; 2012-2014 <a href="https://github.com.jims.emr">EMR</a> All rights reserved.
 */
package com.jims.api.v1.controller.patient;

import com.jims.master.common.data.Message;
import com.jims.master.common.utils.SecurityUtil;
import com.jims.master.common.utils.SendSms;
import com.jims.master.common.web.BaseController;
import com.jims.master.modules.base.user.entity.MasterBaseinfo;
import com.jims.master.modules.base.user.entity.MasterContactinfo;
import com.jims.master.modules.base.user.entity.MasterDoctorinfo;
import com.jims.master.modules.base.user.service.MasterBaseinfoService;
import com.jims.master.modules.base.user.service.MasterContactinfoService;
import com.jims.master.modules.base.user.service.MasterDoctorinfoService;
import com.jims.master.modules.patient.myAttention.entity.ElectronGroupConsultation;
import com.jims.master.modules.patient.myAttention.entity.ElectronGroupConsultationInto;
import com.jims.master.modules.patient.myAttention.service.ElectronGroupConsultationIntoService;
import com.jims.master.modules.patient.myAttention.service.ElectronGroupConsultationService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 会诊记录Controller
 * @author jicunteng
 * @version 2018-06-07
 */
@RestController
@RequestMapping(value = "ConsultationController")
public class ElectronGroupConsultationController extends BaseController {

	@Autowired
	public ElectronGroupConsultationService electronGroupConsultationService;

	@Autowired
	private ElectronGroupConsultationIntoService intoService;

	@Autowired
	private MasterBaseinfoService masterBaseinfoService;


	@Autowired
	private MasterDoctorinfoService masterDoctorinfoService;

	@Autowired
	private MasterContactinfoService masterContactinfoService;


	/**
	 * 保存会诊申请
	 * @param patientId
	 * @param doctorId
	 * @param grouptype
	 * @param bingqingzhaiyao
	 * @param huizhenliyou
	 * @param groupDocs
	 * @return
	 */
	@RequestMapping(value = "save")
	public String saveGroup(String patientId,String doctorId,String grouptype,String bingqingzhaiyao,String huizhenliyou,String groupDocs,String closingDate) {
		Message msg=new Message();
		ElectronGroupConsultation electronGroupConsultation =new ElectronGroupConsultation();
		electronGroupConsultation.setPatientId(patientId);
		electronGroupConsultation.setDoctorId(doctorId);
		electronGroupConsultation.setGrouptype(grouptype);
		electronGroupConsultation.setBingqingzhaiyao(bingqingzhaiyao);
		electronGroupConsultation.setShenqingshijian(new Date());
		electronGroupConsultation.setFabuflag("0");
		electronGroupConsultation.setHuizhenliyou(huizhenliyou);
		DateFormat format= new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date date=format.parse(closingDate);
			electronGroupConsultation.setClosingDate(date);
			int  i = electronGroupConsultationService.saveGroup(electronGroupConsultation,groupDocs);
		/*MasterDoctorinfo fadoc = masterDoctorinfoService.get(doctorId);
		String faname = fadoc.getBaseinfo().getName();*/
			if(i>0){
				msg.setData("会诊申请保存成功");
			}else{
				msg.setData("会诊申请保存失败");
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return SecurityUtil.encryptMsgData(msg);
	}

	@RequestMapping(value = "updateHuizhen")
	public String updateHuizhen(String id,String bingqingzhaiyao,String huizhenliyou,String closingDate) {
		Message msg=new Message();
		ElectronGroupConsultation electronGroupConsultation =electronGroupConsultationService.get(id);
		electronGroupConsultation.setBingqingzhaiyao(bingqingzhaiyao);
		electronGroupConsultation.setHuizhenliyou(huizhenliyou);
		DateFormat format= new SimpleDateFormat("yyyy-MM-dd");
		Date date= null;
		try {
			date = format.parse(closingDate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		electronGroupConsultation.setClosingDate(date);
		int  i = electronGroupConsultationService.save(electronGroupConsultation);
		if(i>0){
			msg.setData("会诊申请修改成功");
		}else{
			msg.setData("会诊申请修改失败");
		}
		return SecurityUtil.encryptMsgData(msg);
	}

	@RequestMapping(value = "fabuhuizhen")
	public String fabuhuizhen(String id) {
		Message msg=new Message();
		ElectronGroupConsultation en = electronGroupConsultationService.get(id);
		en.setFabuflag("1");
		int i = electronGroupConsultationService.save(en);
		if(i>0){
			List<ElectronGroupConsultationInto> list = intoService.getByhuizhen(en.getId());
			for(int j=0;j<list.size();j++){
				MasterDoctorinfo huizhen = masterDoctorinfoService.get(list.get(j).getDoctorId());
				MasterContactinfo huicon = masterContactinfoService.getContacBybaseInfo(huizhen.getBaseinfo().getId());
				String phone =huicon.getMobilePhone();
				MasterDoctorinfo faqi = masterDoctorinfoService.get(en.getDoctorId());
				String huizhenname = huizhen.getBaseinfo().getName();
				if(phone!=null&!"".equals(phone)){
					String sd="尊敬的"+huizhenname+"医生，"+faqi.getBaseinfo().getName()+"医生给您发起了会诊请求，请您及时查看并填写会诊意见，欢迎您使用康需平台。";
					SendSms.sendSmsO(phone,sd);
				}
			}
			msg.setData("会诊申请发布成功");
		}else{
			msg.setData("会诊申请发布失败");
		}
		return SecurityUtil.encryptMsgData(msg);
	}

	/**
	 * 会诊患者列表
	 * @param doctorId
	 * @return
	 */
	@RequestMapping(value = "groupList")
	public String groupList(String doctorId) {
		Message msg=new Message();
		List<ElectronGroupConsultationInto> list =intoService.groupList(doctorId);
		msg.setData(list);
		return SecurityUtil.encryptMsgData(msg);
	}

	@RequestMapping(value = "docgroupList")
	public String docgroupList(String doctorId,String patientId) {
		Message msg=new Message();
		List<ElectronGroupConsultationInto> info =intoService.docgroupList(doctorId,patientId);
		for(int i=0;i<info.size();i++){
			ElectronGroupConsultation en = electronGroupConsultationService.get(info.get(i).getHuizhenId());
			MasterDoctorinfo fa = masterDoctorinfoService.get(en.getDoctorId());
			if(fa!=null){
				info.get(i).setMasterBaseinfo(fa.getBaseinfo());
			}
			info.get(i).setEn(en);
		}
		msg.setData(info);
		return SecurityUtil.encryptMsgData(msg);
	}

	/**
	 * 保存会诊建议
	 * @param huizhenyijian
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "savejianyi")
	public String jianyi(String huizhenyijian,String id) {
		Message msg=new Message();
		ElectronGroupConsultationInto electronGroupConsultationInto =intoService.get(id);
		electronGroupConsultationInto.setHuizhenyijian(huizhenyijian);
		electronGroupConsultationInto.setJianyishijian(new Date());
		electronGroupConsultationInto.setConfim("0");
		int i = intoService.save(electronGroupConsultationInto);
		if(i>0){
			msg.setData("会诊建议保存成功");
		}else{
			msg.setData("会诊建议保存失败");
		}
		return SecurityUtil.encryptMsgData(msg);
	}

	/**
	 * 确认会诊建议，不能修改
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "saveconfirm")
	public String saveconfirm(String id) {
		Message msg=new Message();
		ElectronGroupConsultationInto electronGroupConsultationInto =intoService.get(id);
		electronGroupConsultationInto.setConfim("1");
		electronGroupConsultationInto.setQuerenshijian(new Date());
		int i = intoService.save(electronGroupConsultationInto);
		if(i>0){
			MasterDoctorinfo huizhen = masterDoctorinfoService.get(electronGroupConsultationInto.getDoctorId());
			ElectronGroupConsultation en =electronGroupConsultationService.get(electronGroupConsultationInto.getHuizhenId());
			MasterDoctorinfo faqi = masterDoctorinfoService.get(en.getDoctorId());
			MasterContactinfo huicon = masterContactinfoService.getContacBybaseInfo(faqi.getBaseinfo().getId());
			String phone =huicon.getMobilePhone();
			String huizhenname = huizhen.getBaseinfo().getName();
			if(phone!=null&&!"".equals(phone)){
				String sd="尊敬的"+faqi.getBaseinfo().getName()+"医生，"+huizhenname+"医生已经填写了会诊意见，请您及时查看，欢迎您使用康需平台。";
				SendSms.sendSmsO(phone,sd);
			}
			msg.setData("会诊建议确认成功");
		}else{
			msg.setData("会诊建议确认失败");
		}
		return SecurityUtil.encryptMsgData(msg);
	}

	/**
	 * 填会诊建议list
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "huizhenList")
	public String huizhenList(String id) {
		Message msg=new Message();
		ElectronGroupConsultation electronGroupConsultation =electronGroupConsultationService.getById(id);
		msg.setData(electronGroupConsultation);
		return SecurityUtil.encryptMsgData(msg);
	}
	/**
	 * 历史会诊记录
	 * @param doctorId
	 * @return
	 */
	@RequestMapping(value = "huizhenHistroy")
	public String huizhenHistroy(String doctorId) {
		Message msg=new Message();
		List<ElectronGroupConsultation> list =electronGroupConsultationService.getByDoc(doctorId);
		if(list.size()>0){
			for(int i=0;i<list.size();i++){
				list.get(i).setCount(intoService.getCount(list.get(i).getId()));
				list.get(i).setConfirmCount(intoService.getConfirmCount(list.get(i).getId()));
			}
		}
		msg.setData(list);
		return SecurityUtil.encryptMsgData(msg);
	}





}